package rwlivbk.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import rwlivbk.common.CommonResult;
import rwlivbk.mapper.LiveStreamMapper; // 假设您有一个 LiveStreamMapper
import rwlivbk.model.LiveStream; // 假设您有一个 LiveStream 实体

import java.util.ArrayList;

@RestController
@CrossOrigin
@RequestMapping("/admin-api/live")
public class LiveStreamController {

    private final LiveStreamMapper liveStreamMapper;

    @Autowired
    public LiveStreamController(LiveStreamMapper liveStreamMapper) {
        this.liveStreamMapper = liveStreamMapper;
    }

    @GetMapping("/getAllStreams")
    public CommonResult<?> getAllLiveStreams() {
        ArrayList<LiveStream> liveStreams = liveStreamMapper.findAllLiveStreams();
        if (liveStreams != null) {
            return CommonResult.success(liveStreams);
        } else {
            return CommonResult.error(404, "直播流未找到");
        }
    }

    @GetMapping("/getAllActiveStreams")
    public CommonResult<?> getAllActiveLiveStreams() {
        ArrayList<LiveStream> liveStreams = liveStreamMapper.findAllActiveLiveStreams();
        if (liveStreams != null) {
            return CommonResult.success(liveStreams);
        } else {
            return CommonResult.error(404, "直播流未找到");
        }
    }


    @PostMapping("/add")
    public CommonResult<?> addLiveStream(@RequestBody LiveStream streamData) {
        int result = liveStreamMapper.insertLiveStream(streamData);
        if (result > 0) {
            return CommonResult.success(200);
        } else {
            return CommonResult.error(500, "直播流添加失败");
        }
    }

    @PutMapping("/update")
    public CommonResult<?> updateLiveStreamUrl(@RequestBody LiveStream updatedData) {
        int result = liveStreamMapper.updateLiveStream(updatedData);
        if (result > 0) {
            return CommonResult.success(200);
        } else {
            return CommonResult.error(500, "直播流更新失败");
        }
    }

}
